const { HOME_CONTENT_TYPE } = require('../constants/types');

/**
 * 商品id列表转换为商品列表
 * @param {Array} ids 数组
 * @param {Array} prods 商品列表
 */
const goodsIdsToDetails = (ids, prods) => {
    const result = [];
    for (let i = 0; i < ids.length; i++) {
        const item = prods.filter(temp => temp.id == ids[i])[0];
        result.push(item);
    }
    return result;
};

/**
 * 商城配置信息
 * @param {Object} config 配置信息
 */
const dealShopConfig = (config) => {
    const { decoration_info, prods, page_name } = config;
    const { content } = decoration_info.home;
    let _content = [];
    for (let i = 0; i < content.length; i++) {
        const item = content[i];
        if (item.moduleName === HOME_CONTENT_TYPE.columnContent) {
            _content.push({
                ...item,
                goodsList: goodsIdsToDetails(item.goodsIds, prods),
            });
        } else {
            _content.push(item);
        }
    }
    return {
        global: decoration_info.global,
        startPage: decoration_info.startPage,
        tabbar: decoration_info.tabbar,
        home: { ...decoration_info.home, content: _content },
        special: decoration_info.special,
        cart: decoration_info.cart,
        owner: decoration_info.owner,
        brandName: page_name,
    };
};

module.exports = {
    dealShopConfig,
};